package com.dsteiner.xelavos.server;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dsteiner.widgets.client.DateUtilImpl;
import com.dsteiner.xelavos.client.constants.Constants;
import com.dsteiner.xelavos.client.domain.User;
import com.dsteiner.xelavos.server.dao.DataDao;

public class RegistrationServlet extends HttpServlet {

	private static final Logger log = Logger.getLogger(RegistrationServlet.class.getName());
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		res.setContentType("text/plain");
		String uid = req.getParameter("uid");
		User user = (User) DataDao.dataDao.getObjectById(uid, User.class);
		log.info("received registration request with uid:"+uid + " and found user:"+user);
		if(user != null){
			user.setValidated(true);
			DataDao.dataDao.updateObject(user);
			Cookie cookie = new Cookie(Constants.COOKIE_NAME_USER_ID, user.getId());
			cookie.setPath("/");
			int expiry = (int) (14 * DateUtilImpl.DAY_IN_MS / 1000);
			cookie.setMaxAge(expiry);
			res.addCookie(cookie);
		}
		res.sendRedirect("http://www.xelavos.com");
	}
}